//
//  LauVistorView.swift
//  SwiftProject
//
//  Created by Lausen on 2017/7/17.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class LauVistorView: UIView {

    //4:注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton("注册",
                                                                           fontSize: 16,
                                                                           normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //5:登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton("登录",
                                                                        fontSize: 16,
                                                                        normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    //访客视图的信息字典
    var vistorInfo: [String: String]?{
        
        didSet{
            
            //1:去字典信息
            guard let message = vistorInfo?["message"],
                let imageName = vistorInfo?["imageName"]
                else {
                    return
            }
            
            //2:设置消息内容
            tipLabel.text = message
            
            //3:设置图像视图的内容----首页不需要设置图片了
            if imageName == "" {
                
                startAnimation()
                
                return
            }
            
            houseIconImageView.image = UIImage(named: imageName)
            
            //4:隐藏遮罩视图和圆圈视图
            iconImageView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation() -> (){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        ///动画,完成之后不删除
        //非常重要
        anim.isRemovedOnCompletion = false
        
        iconImageView.layer.add(anim, forKey: nil)
    }
    
    ///懒加载一些视图
    //1:小圈圈
    fileprivate lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //1.5遮罩视图
    fileprivate lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"));
    //2:小房子
    fileprivate lazy var houseIconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //3:提示label
    fileprivate lazy var tipLabel: UILabel = UILabel.cz_label(withText: "关注一些人,关注一些人,关注一些人,关注一些人,关注一些人,关注一些人,", fontSize: 14, color: UIColor.darkGray)
}

//MARK: -- 设置界面
extension LauVistorView{
    
    fileprivate func setupUI(){
        
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        //添加子视图
        addSubview(iconImageView)
        addSubview(maskIconView)
        addSubview(houseIconImageView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipLabel.textAlignment = .center
        
        //取消autoresizing
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let margin: CGFloat = 20
        
        ///使用原生的自动布局
        //1>小圈圈的约束
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        
        //2>小房子的约束
        addConstraint(NSLayoutConstraint(item: houseIconImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //3>tipLabel的约束
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        //4>注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        //5>登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //1.5>遮罩视图的约束
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .bottom,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
    }
}

































